package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zhentao.mapper.AdActivityMapper;
import com.zhentao.model.activity.dto.JoinDto;
import com.zhentao.model.activity.pojo.AdActivePersonnel;
import com.zhentao.model.activity.pojo.AdActivity;
import com.zhentao.model.activity.vo.ActivityVo;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.service.AdActivePersonnelService;
import com.zhentao.mapper.AdActivePersonnelMapper;
import com.zhentao.service.AdActivityService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author
* @description 针对表【ad_active_personnel】的数据库操作Service实现
* @createDate 2024-07-03 14:47:36
*/
@Service
public class AdActivePersonnelServiceImpl extends ServiceImpl<AdActivePersonnelMapper, AdActivePersonnel>
    implements AdActivePersonnelService{
    @Resource
    AdActivePersonnelMapper adActivePersonnelMapper;
    @Resource
    AdActivityMapper adActivityMapper;


    @Override
    public ResponseResult selectAllAc(Integer id) {
        QueryWrapper<AdActivePersonnel> wrapper=new QueryWrapper<>();
        wrapper.lambda().eq(AdActivePersonnel::getActivityId,id);
        List<AdActivePersonnel> adActivePersonnels = adActivePersonnelMapper.selectList(wrapper);
        if (ObjectUtils.isEmpty(adActivePersonnels)){
            AdActivity adActivity = adActivityMapper.selectById(id);
            return ResponseResult.okResult(adActivity);
        }
        List<ActivityVo> activityVos= adActivePersonnelMapper.selectAllAc(id);
        return ResponseResult.okResult(activityVos);
    }

    @Override
    public ResponseResult add(AdActivePersonnel adActivePersonnel) {
        adActivePersonnel.setApplyTime(new Date());
        adActivePersonnelMapper.insert(adActivePersonnel);
        return ResponseResult.okResult("报名成功");
    }

    @Override
    public ResponseResult selectAllUser(Integer id) {
        List<JoinDto> activityVos= adActivePersonnelMapper.selectAllUser(id);
        for (JoinDto joinDto:activityVos){
            QueryWrapper<AdActivePersonnel> wrapper=new QueryWrapper<>();
            wrapper.lambda().eq(AdActivePersonnel::getActivityId,joinDto.getId());
            wrapper.lambda().eq(AdActivePersonnel::getUserId,id);
            AdActivePersonnel adActivePersonnel = adActivePersonnelMapper.selectOne(wrapper);
            if (adActivePersonnel!=null){
                joinDto.setJoinStatus(1);
            }else {
                joinDto.setJoinStatus(0);
            }

        }
        return ResponseResult.okResult(activityVos);
    }
}




